//
//  Second.swift
//  mng
//
//  Created by WTFKL on 2018/4/3.
//  Copyright © 2018年 WTF. All rights reserved.
//


import GameplayKit
import SpriteKit

class Second: GameScene {
    private var partition : SKSpriteNode?;
    private var partition2 : SKSpriteNode?;
    private var partition3 : SKSpriteNode?;
    private var partition4 : SKSpriteNode?;
    private var groundPart2 : SKSpriteNode?;
    private var groundPart3 : SKSpriteNode?;
    private var keySprite : SKSpriteNode?;
    private var realkeySprite : SKSpriteNode?;
    private var ship : SKSpriteNode?;
    private var ball : SKSpriteNode?;
    
    override func didMove(to view: SKView) {
        super.didMove(to: view);
        initWorld();
        
    }
    
    func initWorld(){
       
        tips?.text = "提示：或许你该注意脚底？";
        let groundWidth = phoneType! ? 250 : 280;
        ground?.size = CGSize(width: groundWidth, height: 100);
        ground?.position = CGPoint(x: (-screenWidth+(ground?.size.width)!)/2, y: (ground?.position.y)!);
        ground?.physicsBody = SKPhysicsBody(rectangleOf: (ground?.size)!);
        ground?.physicsBody?.affectedByGravity = false;
        ground?.physicsBody?.isDynamic = false;
        ground?.physicsBody?.restitution = 0;
        ground?.physicsBody?.categoryBitMask = BitMaskType.ground;
        ground?.physicsBody?.contactTestBitMask = BitMaskType.charactor;//能被角色碰撞
        ground?.zPosition = -1;
        //隔板
        partition = SKSpriteNode(color: .black, size: CGSize.init(width: 100, height: 20));
        partition?.position = CGPoint(x: -190, y: 25);
        partition?.physicsBody = SKPhysicsBody(rectangleOf: (partition?.frame.size)!);
        partition?.physicsBody?.categoryBitMask = BitMaskType.board;
        partition?.physicsBody?.contactTestBitMask = BitMaskType.charactor;
        partition?.physicsBody?.affectedByGravity = true;
        partition?.physicsBody?.isDynamic = false;
//        partition?.isHidden = true;
        self.addChild(partition!);
        let partition2Y = phoneType! ? -30 : -20;
        partition2 = SKSpriteNode(color: .black, size: CGSize.init(width: 50, height: 20));
        partition2?.position = CGPoint(x: -80, y: partition2Y);
        partition2?.physicsBody = SKPhysicsBody(rectangleOf: (partition2?.frame.size)!);
        partition2?.physicsBody?.categoryBitMask = BitMaskType.board;
        partition2?.physicsBody?.contactTestBitMask = BitMaskType.charactor;
        partition2?.physicsBody?.affectedByGravity = false;
        partition2?.physicsBody?.isDynamic = false;
        self.addChild(partition2!);
        
        partition3 = SKSpriteNode(color: .black, size: CGSize.init(width: 100, height: 20));
        partition3?.position = CGPoint(x: 50, y: 112);
        partition3?.physicsBody = SKPhysicsBody(rectangleOf: (partition3?.frame.size)!);
        partition3?.physicsBody?.categoryBitMask = BitMaskType.board;
        partition3?.physicsBody?.contactTestBitMask = BitMaskType.charactor;
        partition3?.physicsBody?.affectedByGravity = true;
        partition3?.physicsBody?.isDynamic = false;
//        partition3?.isHidden = true;
        self.addChild(partition3!);
        
        partition4 = SKSpriteNode(color: .black, size: CGSize.init(width: 20, height: 20));
        partition4?.position = CGPoint(x: -70, y: 82);
        partition4?.physicsBody = SKPhysicsBody(rectangleOf: (partition4?.frame.size)!);
        partition4?.physicsBody?.categoryBitMask = BitMaskType.board;
        partition4?.physicsBody?.contactTestBitMask = BitMaskType.charactor;
        partition4?.physicsBody?.affectedByGravity = true;
        partition4?.physicsBody?.isDynamic = false;
//        partition4?.isHidden = true;
        self.addChild(partition4!);
        
        let groundPart3Width = phoneType! ? 280 : 320 ;
        groundPart3 = SKSpriteNode(color: .black, size: CGSize.init(width: groundPart3Width, height: 100));
        groundPart3?.position = CGPoint(x: (ground?.position.x)!+(ground?.size.width)!/2+(groundPart3?.size.width)!/2, y: (ground?.position.y)!);
        groundPart3?.physicsBody = SKPhysicsBody(rectangleOf: (groundPart3?.frame.size)!);
        groundPart3?.physicsBody?.categoryBitMask = BitMaskType.SpecialGround;
        groundPart3?.physicsBody?.contactTestBitMask = BitMaskType.charactor;
        groundPart3?.physicsBody?.collisionBitMask = BitMaskType.damageThing;
        groundPart3?.physicsBody?.affectedByGravity = false;
        groundPart3?.physicsBody?.mass = 80;
        groundPart3?.physicsBody?.isDynamic = false;
        groundPart3?.zPosition = -1;
        groundPart3?.physicsBody?.restitution = 0;
        self.addChild(groundPart3!);
        
        groundPart2 = SKSpriteNode(color: .black, size: CGSize.init(width: (screenWidth - (groundPart3?.size.width)! - (ground?.size.width)!), height: 100));
        groundPart2?.position = CGPoint(x:(screenWidth-(groundPart2?.size.width)!)/2,y:(ground?.position.y)!);
        groundPart2?.physicsBody = SKPhysicsBody(rectangleOf: (groundPart2?.frame.size)!);
        groundPart2?.physicsBody?.categoryBitMask = BitMaskType.ground;
        groundPart2?.physicsBody?.contactTestBitMask = BitMaskType.charactor;
        groundPart2?.physicsBody?.affectedByGravity = true;
        groundPart2?.physicsBody?.isDynamic = false;
        groundPart2?.zPosition = -1;
        groundPart2?.physicsBody?.restitution = 0;
        self.addChild(groundPart2!);
        
        
        ship = SKSpriteNode(color: .clear, size: CGSize.init(width: (ground?.size.height)!, height: (ground?.size.height)!));
        ship?.position = CGPoint(x: (groundPart2?.position.x)! - (groundPart2?.size.width)!/2 - (ship?.size.width)!/2, y: (ground?.position.y)!);
        ship?.physicsBody = SKPhysicsBody(rectangleOf: (ship?.frame.size)!);
        ship?.physicsBody?.categoryBitMask = BitMaskType.SpecialGround;
        ship?.physicsBody?.contactTestBitMask = BitMaskType.charactor;
        ship?.physicsBody?.collisionBitMask = BitMaskType.charactor;
        ship?.physicsBody?.affectedByGravity = false;
        ship?.physicsBody?.isDynamic = false;
        ship?.zPosition = -2;
        
        
        
        //key
        keySprite = MeKey(mName: "key", posistion: CGPoint(x: (groundPart2?.position.x)!, y: (groundPart2?.position.y)!+80));
        self.addChild(keySprite!);
        
    }
    
    override func didBegin(_ contact: SKPhysicsContact) {
        super.didBegin(contact);
        if (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask == BitMaskType.charactor|BitMaskType.SpecialGround ) {
            isOnGround = false;
            if(contact.bodyB.node == ship){//接触到移动块
                isOnGround = true;
                ship?.color = .black;
                let moveAction = SKAction.move(to: CGPoint(x: (ground?.position.x)! + (ground?.size.width)!/2 + (ship?.size.width)!/2, y: (ship?.position.y)!), duration: 3);
                ship?.run(moveAction);
            }else {
                groundPart3?.removeFromParent();
                isOnGround = true;
            }
        }
        
        if (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask) == (BitMaskType.charactor | BitMaskType.key) {//人物与钥匙
            
//            let mykeyNode = contact.bodyB.node as? MeKey;
            
            if(contact.bodyB.node == keySprite){
                keySprite?.removeFromParent();
                self.addChild(ship!);
                realkeySprite = MeKey(mName: "realkey", posistion: CGPoint(x: (ground?.position.x)!, y: (groundPart2?.position.y)!+80));
                self.addChild(realkeySprite!);
            }else if (contact.bodyB.node == realkeySprite){
                mesprite?.isGetKey = true;
                realkeySprite?.removeFromParent();
            }
        }
        
        if (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask) == (BitMaskType.charactor | BitMaskType.other) {//人物与过关触发器
            let successNode = contact.bodyB.node as? SKSpriteNode;
            if(successNode?.name == "success"){
                self.view?.presentScene(SKScene(fileNamed: "Third")!,transition: SKTransition.crossFade(withDuration: 0.5));
            }
        }
    }
    
    func didEnd(_ contact: SKPhysicsContact) {
        if (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask == BitMaskType.line|BitMaskType.SpecialGround ) {
            isOnGround = true;
        }
    }
    
    func charactorIsAlive(){
        if (mesprite?.position.y)! <= -157{
            mesprite?.isAlive = false;
        }
        if mesprite?.isAlive == false {
            self.view?.presentScene(SKScene(fileNamed: "Second")!,transition: SKTransition.crossFade(withDuration: 0.5));
        }
    }
    
    override func update(_ currentTime: TimeInterval) {
        super.update(currentTime);
        charactorIsAlive();
    }
}
